﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Application;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Logic.Web;
using LotteryVote.Utility;

namespace LotteryVote
{
    public partial class OrderManager : CompanyPage
    {
        BetFacade betFacade = new BetFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }

        void InitParams()
        {
            lv_pager.InitPager();
            if (!IsPostBack)
                BindOrder();
        }

        private void BindOrder()
        {
            int totalCount;
            rpt_betOrder.DataSource = GetOrderList(out totalCount);
            rpt_betOrder.DataBind();
            lv_pager.BindPager(totalCount, LotterySystem.Current.PageItemCount);
        }
        IList<BetOrder> GetOrderList(out int totalCount)
        {
            IList<BetOrder> result;
            if (lv_orderSearch.IsSearched)
            {
                result = betFacade.FindTodayOrder(lv_orderSearch.Num, lv_orderSearch.CompanySelectedValue, lv_orderSearch.GamePlayWaySelectedValue,
                    lv_orderSearch.SortField, lv_orderSearch.SortType, LotterySystem.Current.PageItemCount, lv_pager.CurrentPager, out totalCount);
            }
            else
            {
                result = betFacade.GetTodayValidOrder(LotterySystem.Current.PageItemCount, lv_pager.CurrentPager, out totalCount);
            }
            return result;
        }

        decimal amount, turnover, comm, netAmount;
        protected void rpt_betOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                BetOrder data = e.Item.DataItem as BetOrder;
                if (data != null)
                {
                    turnover += data.Turnover;
                    comm += data.Commission;
                    netAmount += data.NetAmount;
                    amount += data.Amount;
                }
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                Literal ltlturnover = e.Item.FindControl("ltl_turnover") as Literal;
                Literal ltlcommisson = e.Item.FindControl("ltl_comm") as Literal;
                Literal ltlnetAmount = e.Item.FindControl("ltl_netAmount") as Literal;
                Literal ltlamont = e.Item.FindControl("ltl_amount") as Literal;

                if (ltlturnover != null && ltlcommisson != null && ltlnetAmount != null)
                {
                    ltlturnover.Text = turnover.ToString();
                    ltlcommisson.Text = comm.ToString();
                    ltlnetAmount.Text = netAmount.ToString();
                    ltlamont.Text = amount.ToString();
                }

            }
        }
        protected void rpt_betOrder_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "cancel")
            {
                int orderId = Convert.ToInt32(e.CommandArgument);
                betFacade.CancelBetOrder(orderId, CurrentUser);
                BindOrder();
            }
        }

        public GamePlayWay FindGamePlayWay(object gpwId)
        {
            return LotterySystem.Current.FindGamePlayWay(Convert.ToInt32(gpwId));
        }
        public LotteryCompany FindCompany(object companyId)
        {
            return TodayLotteryCompany.Instance.GetTodayCompany().Find(it => it.CompanyId == Convert.ToInt32(companyId));
        }
    }
}